<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Detail extends MX_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 * 	- or -
	 * 		http://example.com/index.php/welcome/index
	 * 	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
	function Detail() {
		parent::__construct();
		$this->load->model('lookupmodel');
		$this->load->model('helpermodel');
		//check is logged in
		$this->load->model('languagemodel');
		$this->load->model('email/emailmodel');
		$this->language = $this->languagemodel->is_language_using();
		$this->load->helper('MY_url_helper');
		$this->load->helper('utf8');
		$this->load->model('imagemodel');
		
		$this->load->model('banners/bannermodel');
	}
	
	public function index($url) {
		$this->lang->load('listing/detail', $this->config->item('language'));

		$this->load->model('listingmap/mdl_listingmapmodel');
		$options = array();
		
		$parts = explode('-', $url);
        $id = end($parts);

		//$id = $this->input->get('id');

		if(!isset($id) || !is_numeric($id) || $id < 0) {
			redirect(base_url() . 'error_404', 'refresh');
		}

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_listing_result_heading'),
			'href'      => base_url() . 'listing-rows.html?localorint=0&propertyfor=sale'
		);
		
		//$listingtoview['media'] = $this->mdl_listingmapmodel->get_media(128, "Gallery", 20);
		
		//$thumb = $this->imagemodel->resize('3251.jpg', THUMB_IMAGE_WIDTH, THUMB_IMAGE_HEIGHT, HTTP_PROJECT_LISTING_IMAGE . 128 . '/', $this->config->item('HTTP_LISTING_MEDIA_IMAGE') . 128 . '/');

		$options['localorInt'] = NULL;
		$options['market'] = NULL;
		$options['country'] = NULL;
		$options['cities'] = NULL;
		$options['locations'] = NULL;
		$options['subarea'] = NULL;
		$options['list_type'] = NULL;
		$options['propertyfor'] = NULL;
		$options['location'] = NULL;
		$options['propertytype'] = NULL;
		$options['price_min'] = NULL;
		$options['price_max'] = NULL;
		$options['size_min'] = NULL;
		$options['size_max'] = NULL;
		$options['bedroom_min'] = NULL;
		$options['bedroom_max'] = NULL;
		$options['bathroom_min'] = NULL;
		$options['bathroom_max'] = NULL;
		$options['keyword'] = NULL;
		$options['orderby'] = "ListedOnUtc DESC";
		$options['features'] = NULL;
		$options['facilities'] = NULL;
		$options['tenu'] = NULL;
		$options['language'] = NULL;
		$options['listing_id'] = $id;

		if ($this->input->get('page')) {
			$options['page'] = $this->input->get('page');
		} else {
			$options['page'] = 1;
		}

		$options['offset'] = NULL;
		$options['limit'] = NULL;

		//$results = $this->mdl_listingmapmodel->getListingWithWhere($options);
		$results = $this->mdl_listingmapmodel->getProjectListing($options);

		if(isset($results) && is_array($results) && count($results) > 0) {
			$data['listingtoview'] = $results[0];
		} else {
			redirect(base_url() . 'error_404', 'refresh');
		}

		$data['breadcrumbs'][] = array(
			'text'      => $data['listingtoview']['ProjectName'],
			'href'      => base_url() . 'detail/' . $url
		);
		
		//SEO
		$data['title'] = $data['listingtoview']['PageTitle'];
		$data['description'] = $this->lang->line('listing_detail_text_description');
		$data['keywords'] = $this->lang->line('listing_detail_text_keywords');

		//get Features using Listing ID
		$data['features'] = array();
		$results = $this->mdl_listingmapmodel->getFeatures($id, $this->config->item('config_language_id'),$this->config->item('market'));

		if($results) {
			foreach ($results as $result) {
				$data['features'][] = array(
				'Features_ID'	=> $result->Features_ID,
				'Name'			=>	$result->Features
				);
			}
		} else {
			$data['features'] = false;
		}

		//get Facilities using Listing ID
		$data['Facilities'] = array();
		$results = $this->mdl_listingmapmodel->getFacilities($id, $this->config->item('config_language_id'),$this->config->item('market'));

		if($results) {
			foreach ($results as $result) {
				$data['Facilities'][] = array(
				'Facilities_ID'	=> $result->Facilities_ID,
				'Name'			=>	$result->Facilities
				);
			}
		} else {
			$data['Facilities'] = false;
		}

		//get Agent Information using Listing ID
		$this->load->model('member/mdl_member');
		$this->load->model('findanexpert/mdl_findanexpert');
		
		$data['agentInfo'] = array();
		$result = $this->mdl_member->getAgentInfo($id, $this->config->item('config_language_id'),$this->config->item('market'));
		
		if($result) {
			$user_id = $this->mdl_findanexpert->getUser($id);
			$agentPhoto = $this->mdl_findanexpert->getAgentPhoto($user_id);
			
			$mobile = $result->Mobile;
			$mobile = str_replace(MOBILE_FIND_EXT, MOBILE_EXT, $mobile);
			
			$data['agentInfo'] = array(
				'Agent_ID'		=> $result->User_ID,
				'Name'			=> $result->BusinessName,
				'Mobile'		=> $mobile,
				'Email'			=> $result->Email,
				'AgentPhoto'	=> $agentPhoto,
				'RegistrationNo'=> $result->CEA_RegistrationNo,
				'LicenceNo'		=> $result->CEA_LicenceNo
			);
		} else {
			$data['agentInfo'] = false;
		}

		//get similar Listing
		$data['similar_listings'] = array();
		
		$results = $this->mdl_listingmapmodel->getProductRelated($id);
		
		if($results) {
			foreach ($results as $result) {
				$listingtoview['media'] = $this->mdl_listingmapmodel->get_media($result->Listing_ID, "Gallery", 1);
				if($listingtoview['media']) {
					$image = $this->imagemodel->resize($listingtoview['media']->FileName . $listingtoview['media']->FileExtension, THUMB_IMAGE_WIDTH, THUMB_IMAGE_HEIGHT, HTTP_PROJECT_LISTING_IMAGE . $result->Listing_ID . '/', $this->config->item('HTTP_LISTING_MEDIA_IMAGE') . $result->Listing_ID . '/');	
				} else {
					$image = false;
				}
				
				$location = "";
				if(!empty($result->City)) {
					$location .= $result->City;
				}
				
				if(!empty($result->Location)) {
					$location .= ', ' . $result->Location;
				}
				
				if(!empty($result->Subarea)) {
					$location .= ', ' . $result->Subarea;
				}
				
				$price = "";
				if($result->Price_Min > 0) {
					$price = $result->PriceUnit . " " . number_format(($result->Price_Min), 0, '.', ',');
				} else {
					$price = "Price On Application";
				}
				
				$data['similar_listings'][] = array(
					'listing_id'	=> $result->Listing_ID,
					'thumb'			=> $image,
					'name'			=> $result->ProjectName,
					'location'		=> $location,
					'price'			=> $price
				);
			}
		}
		
		//most viewed property listing
		$data['most_viewed_listings'] = array();
		
		$results = $this->mdl_listingmapmodel->getMostViewed();
		
		foreach ($results as $result) {
			$listingtoview['media'] = $this->mdl_listingmapmodel->get_media($result->Listing_ID, "Gallery", 1);
			if($listingtoview['media']) {
				$image = $this->imagemodel->resize($listingtoview['media']->FileName . $listingtoview['media']->FileExtension, THUMB_IMAGE_WIDTH, THUMB_IMAGE_HEIGHT, HTTP_PROJECT_LISTING_IMAGE . $result->Listing_ID . '/', $this->config->item('HTTP_LISTING_MEDIA_IMAGE') . $result->Listing_ID . '/');	
			} else {
				$image = false;
			}
			
			$location = "";
			if(!empty($result->City)) {
				$location .= $result->City;
			}
			
			if(!empty($result->Location)) {
				$location .= ', ' . $result->Location;
			}
			
			if(!empty($result->Subarea)) {
				$location .= ', ' . $result->Subarea;
			}
			
			$price = "";
			if($result->Price_Min > 0) {
				$price = $result->PriceUnit . " " . number_format(($result->Price_Min), 0, '.', ',');
			} else {
				$price = "Price On Application";
			}
			
			$data['most_viewed_listings'][] = array(
				'listing_id'	=> $result->Listing_ID,
				'thumb'			=> $image,
				'name'			=> $result->ProjectName,
				'location'		=> $location,
				'price'			=> $price
			);
		}
		
		//update viewed
		$this->mdl_listingmapmodel->updateViewed($id);

		// -- START FACEBOOK COMMENTS META TAGS
		$this->load->library('document');
		$current_url = base_url() . 'detail.html?id=' . $data['listingtoview']['Listing_ID'];
		$data['app_id']         = $this->config->item('facebook_comments_app_id');
		$data['width']          = 780;
		$data['posts']          = 10;
		$data['color_scheme']   = 'light';
		$data['heading_title'] = $this->config->item('facebook_comments_heading_title');
		$this->document->addFBMeta('fb:app_id', $this->config->item('facebook_comments_app_id'));
		$this->document->addFBMeta('og:title', $data['listingtoview']['ProjectName']);
		$this->document->addFBMeta('og:type', 'detail');
		$this->document->addFBMeta('og:url', base_url() . 'detail.html?id=' . $data['listingtoview']['Listing_ID']);
		$this->document->addFBMeta('og:description', trim(strip_tags(html_entity_decode($data['listingtoview']['Description'], ENT_QUOTES, 'UTF-8'))));

		$data['fbmetas'] = $this->document->getFBMetas();

		$data['current_url'] = $current_url;
		// -- STOP FACEBOOK COMMENTS META TAGS
		
		//get banner for right side
		$data['banner'] = $this->bannermodel->getBanners('Detail', $this->config->item('market'), $this->config->item('config_language_id'));

		$data['module'] = $this->uri->segment(1);
		$data['view_file'] = "detail";

		echo Modules::run('template/default_template', $data);
	}
        
        
        public function map_pop($url)
        {
        	$this->load->model('listingmap/mdl_listingmapmodel');
			$options = array();
			
        	$parts = explode('-', $url);
        	$id = end($parts);
        	
        	//$id = $this->input->get('id');
           	
            //get property by id
	        $options['localorInt'] = NULL;
			$options['market'] = NULL;
			$options['country'] = NULL;
			$options['cities'] = NULL;
			$options['locations'] = NULL;
			$options['subarea'] = NULL;
			$options['list_type'] = NULL;
			$options['propertyfor'] = NULL;
			$options['location'] = NULL;
			$options['propertytype'] = NULL;
			$options['price_min'] = NULL;
			$options['price_max'] = NULL;
			$options['size_min'] = NULL;
			$options['size_max'] = NULL;
			$options['bedroom_min'] = NULL;
			$options['bedroom_max'] = NULL;
			$options['bathroom_min'] = NULL;
			$options['bathroom_max'] = NULL;
			$options['keyword'] = NULL;
			$options['orderby'] = "ListedOnUtc DESC";
			$options['features'] = NULL;
			$options['facilities'] = NULL;
			$options['tenu'] = NULL;
			$options['language'] = NULL;
			$options['listing_id'] = $id;
	
			if ($this->input->get('page')) {
				$options['page'] = $this->input->get('page');
			} else {
				$options['page'] = 1;
			}
	
			$options['offset'] = NULL;
			$options['limit'] = NULL;
	
			//$results = $this->mdl_listingmapmodel->getListingWithWhere($options);
			$results = $this->mdl_listingmapmodel->getProjectListing($options);
	
			if(isset($results) && is_array($results) && count($results) > 0) {
				$data['listingtoview'] = $results[0];
			} else {
				redirect(base_url() . 'error_404', 'refresh');
			}
            
            $data['module'] = "detail";
            $data['view_file'] = "map_pop";

            echo Modules::run('template/one_col', $data);
        }
        
	function getAgentProfilePhoto() {
		$assoc_id =$this->uri->segment(3,0);
		
		$this->load->model('findanexpert/mdl_findanexpert');
		$result = $this->mdl_findanexpert->getAgentPhotoUsingUserId($assoc_id);

		if($result) {
			header('Content-Type: image/jpeg');
			echo $result->Photo;
		} else {
			echo $this->config->item('HTTP_AGENTS_MEDIA_IMAGE') . "no-image.png";
		}
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */